// 封装category二级分类业务代码
import { categoryGetAPI } from '@/apis/category.js'
import { ref } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'

export const useCategory = () => {
  // 使用一级分类id，获取二级分类数据
  const categoryData = ref({})
  const route = useRoute()

  const getCategoryData = async (id = route.params.id) => {
    const res = await categoryGetAPI(id)
    categoryData.value = res.result
  }

  getCategoryData()

  // 路由更新时，重新获取二级分类数据
  onBeforeRouteUpdate(async (to) => {
    getCategoryData(to.params.id)
  })

  return { categoryData }
}
